<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Hmf_Errors
 */
require_once 'Hmf/Errors.php';

/**
 * @see Hmf_Logs
 */
require_once 'Hmf/Logs.php';

/**
 * Hmf_Instance
 *
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Class
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Instance.php 125 2009-01-16 20:48:16Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

abstract class Hmf_Instance {

    final public static function getModel($className) {

        $classPath = PATH_DIR_APPLICATION_MODELS . '/' . $className . '.php';

        if (file_exists($classPath)) {

            require_once $classPath;
        } else {

            Hmf_Logs::sendWarningToLog("Model $className's file not exits.");
        }

        $strClassName = 'model_' . $className;

        if (!@class_exists($strClassName))
            Hmf_Errors::missingModel($strClassName);

        try {

            $class = new ReflectionClass($strClassName);

            if (preg_match('/^Hmf_Model_/', $class->getParentClass()->getName())) {

                return $class->newInstance();

            } else {

                Hmf_Errors::classNotExtends('Hmf_Model', $className);

                return false;
            }

        } catch (Zend_Exception $e) {

            Hmf_Errors::showError($e->getCode(), 'getInstanceModel Error', $e->getMessage(), 'error');

        }
        return false;
    }

    final public static function getComponent($className, array $args = array()) {

        $classPath = PATH_DIR_LIB.'/Hmf/Components/' . $className . '.php';

        if (file_exists($classPath)) {

            require_once $classPath;
        } else {

            Hmf_Logs::sendWarningToLog("Components $className's file not exits.");
        }

        $strClassName = 'Hmf_Components_' . $className;

        if (!class_exists($strClassName)) {

            Hmf_Errors::missingComponent($strClassName);
            return false;
        }

        try {

            $class = new ReflectionClass($strClassName);

            if ($class->hasMethod('__construct')) {
                return $class->newInstanceArgs($args);
            } else {
                return $class->newInstance();
            }

        } catch (Exception $e) {

            Hmf_Errors::showError($e->getCode(), 'Controller class', $e->getMessage(), 'error');
        }
        return false;
    }
}

?>
